package com.lagou.sqlSession;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;

import java.lang.reflect.*;
import java.util.List;

public class DefaultSqlSession implements  SqlSession{
    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        //使用动态代理为dao接口生成代理对象，并返回
        return (T)Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(),
                new Class[]{mapperClass}, new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Exception {

                        String methodName = method.getName();
                        String className = method.getDeclaringClass().getName();
                        String statementId = className+"."+methodName;

                        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
                        String tagType = mappedStatement.getTagType();
                        switch (tagType) {
                            case "select":
                                Type genericReturnType = method.getGenericReturnType();
                                //判断是否实现泛型类型参数化
                                if(genericReturnType instanceof ParameterizedType){
                                    return selectList(mappedStatement, args);
                                }else{
                                    return selectOne(mappedStatement,args);
                                }
                            case "insert":
                                return insert(mappedStatement,args);
                            case "update":
                                return update(mappedStatement,args);
                            case "delete":
                                return delete(mappedStatement,args);
                        }
                        return null;
                    }
                });

    }


    @Override
    public <E> List<E> selectList(MappedStatement mappedStatement, Object... params) throws Exception {
        //将要去完成SimpleExcutor的invoke方法的调用：基于开闭原则，先创建接口
        Executor simpleExecutor = new SimpleExecutor();
        return simpleExecutor.query(configuration, mappedStatement, params);

    }

    @Override
    public <T> T selectOne(MappedStatement mappedStatement, Object... params) throws Exception {
        List<Object> objects = selectList(mappedStatement, params);
        if (objects.size()==1){
            return (T)objects.get(0);
        }else{
            throw new RuntimeException("查询结果为空，或返回结果过多");
        }
    }

    //数据添加方法
    @Override
    public int insert(MappedStatement mappedStatement,Object... params) throws Exception {
        Executor simpleExecutor = new SimpleExecutor();
        return simpleExecutor.insert(configuration, mappedStatement, params);
    }

    //数据修改方法
    @Override
    public int update(MappedStatement mappedStatement,Object... params) throws Exception {
        Executor simpleExecutor = new SimpleExecutor();
        return simpleExecutor.update(configuration, mappedStatement, params);
    }
    //数据删除方法
    @Override
    public int delete(MappedStatement mappedStatement,Object... params) throws Exception {
        Executor simpleExecutor = new SimpleExecutor();
        return simpleExecutor.delete(configuration, mappedStatement, params);
    }
}
